package com.zzzzzz.core.dao;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.zzzzzz.core.exception.SystemException;

public class BaseDao {
	@Resource
	protected NamedParameterJdbcTemplate jdbcTemplate;
	
	protected <T> long updateForLongKey(final String sql,T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    KeyHolder keyHolder = new GeneratedKeyHolder();  
	    jdbcTemplate.getJdbcOperations().update(sql, param, keyHolder);
	    return keyHolder.getKey().longValue();
	}
	
	protected <T> int updateForIntKey(final String sql,T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    KeyHolder keyHolder = new GeneratedKeyHolder();  
	    jdbcTemplate.getJdbcOperations().update(sql, param, keyHolder);
	    return keyHolder.getKey().intValue();
	}
	
	protected <T> void update(final String sql,T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    jdbcTemplate.getJdbcOperations().update(sql, param);
	}
	
	protected <T> void update(final String sql, Object... obj){
	    jdbcTemplate.getJdbcOperations().update(sql, obj);
	}
	
	protected <T> T findOne(final String sql,RowMapper<T> rowMapper){
		return jdbcTemplate.getJdbcOperations().queryForObject(sql, rowMapper);
	}
	
	protected <T> T findOne(final String sql,RowMapper<T> rowMapper,Object... obj){
		return jdbcTemplate.getJdbcOperations().queryForObject(sql, rowMapper, obj);
	}
	
	protected String findString(final String sql){
		return jdbcTemplate.getJdbcOperations().queryForObject(sql, String.class);
	}
	
	protected String findString(final String sql,Object... obj){
		return jdbcTemplate.getJdbcOperations().queryForObject(sql, String.class, obj);
	}

	public int findInt(String sql){
		return jdbcTemplate.getJdbcOperations().queryForInt(sql);
	}
	
	public int findInt(String sql,Object... obj){
		return jdbcTemplate.getJdbcOperations().queryForInt(sql,obj);
	}
	
	public long findLong(String sql){
		return jdbcTemplate.getJdbcOperations().queryForLong(sql);
	}
	
	public long findLong(String sql,Object... obj){
		return jdbcTemplate.getJdbcOperations().queryForLong(sql,obj);
	}
	
	public <T> List<T> findList(final String sql, RowMapper<T> mapper) {
		List<T> results = jdbcTemplate.getJdbcOperations().query(sql, mapper);
		return results;
	}
	 
	public <T> List<T> findList(final String sql, RowMapper<T> rowMapper, Object... obj) {
		List<T> results = null;
		results = jdbcTemplate.getJdbcOperations().query(sql, rowMapper, obj);
		return results;
	 }
	
	public SystemException processException(Exception e) {
		if (!(e instanceof DataAccessException)) {
			logger.error("Caught unexpected exception " + e.getClass().getName());
		}

		if (logger.isDebugEnabled()) {
			logger.debug(e.getMessage(), e);
		}

		return new SystemException(e);
	}

	private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);
	
}
